package cn.skywalks.common.utils;

import java.util.Random;

/**
 * Created by lpeng on 2017/2/20.
 */
public class RandomUtil {

    private static final String chars  = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
    private static final String nums  = "0123456789";

    /**
     * 获取指定长度的随机字符串
     * @param length    指定长度
     * @return  生成的随机字符串
     */
    public static String getRanChars(int length){
        return getRanString(chars, length);
    }

    /**
     * 获取指定长度的随机数字
     * @param length    指定长度
     * @return  生成的随机数字
     */
    public static  String getRanNums(int length){
        return getRanString(nums, length);
    }

    /**
     * 根据baseStr获取指定长度的随机字符串
     * @param baseStr   基数
     * @param length    指定长度
     * @return  返回的字符串
     */
    private static String getRanString(String baseStr, int length){
        Random random = new Random();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i <length ; i++) {
            int index = random.nextInt(baseStr.length());
            char c = baseStr.charAt(index);
            builder.append(c);
        }
        return  builder.toString();
    }

}
